//
//  SelectModelView.m
//  答题模式控件
//
//  Created by dcr on 2017/1/18.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "SelectModelView.h"

@implementation SelectModelView
{
    SelectTouch block;  //代码块
}

- (instancetype)initWithFrame:(CGRect)frame andTouch:(SelectTouch)touch{
    self = [super initWithFrame:frame];
    if(self){
        [self createUI];
        block = touch;
        _model = testModel;
    }
    return self;
}

- (void)createUI{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    NSArray *arr = [[NSArray alloc]initWithObjects:@"答题模式", @"背题模式", nil];
    for(int i = 0; i < arr.count; i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //位置水平居中，垂直向上移150
        btn.frame = CGRectMake(self.frame.size.width/2-50, self.frame.size.height/2-200 + i*130, 100, 100);
        btn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        btn.tag = 400+i;
        //设置圆角
        btn.layer.masksToBounds = true;
        btn.layer.cornerRadius = 8;
        //添加点击事件
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 60)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d11q.png", i+1]];
        [btn addSubview:imageView];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 75, 80, 20)];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = arr[i];
        [btn addSubview:label];
        [self addSubview:btn];
    }
}

//点击按钮响应事件，根据TAG区分
- (void)click:(UIButton *)sender{
    if(sender.tag == 400){
        _model = testModel;
    }else if(sender.tag == 401){
        _model = lookingModel;
    }
    block(_model);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }];
}

//点击其它区域，隐藏界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }];
}
@end
